package com.lv.controller;

import com.lv.data.Result;
import com.lv.pojo.TbItemCat;
import com.lv.service.CatService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("cat")
public class ItemCatController {

    @DubboReference
    private CatService cs;

    @RequestMapping("toCat")
    public String toCat(){
        return "admin/item_cat";
    }


    @RequestMapping("selectParendId")
    @ResponseBody
    public Result getInfo(Long parentId){
        try {
            List<TbItemCat> list = cs.selectParendId(parentId);
            return Result.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("selectID")
    @ResponseBody
    public Result selectID(Long id) {
        try {
            TbItemCat list = cs.selectID(id);
            return Result.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

    //增加
    @RequestMapping("add")
    @ResponseBody
    public Result add(TbItemCat c) {
        try {
            if (c.getParentId() == null) {
                c.setParentId(Long.valueOf(0));
            }
            Integer i = cs.add(c);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }

    //删除
    @RequestMapping("del")
    @ResponseBody
    public Result del(@RequestParam("ids") List<Long> ids){
        try {
           cs.del(ids);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }
    //回显
    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate( Long id){
        try {
            TbItemCat c =cs.toupdate(id);
            return Result.OK().setData(c);
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }
    @RequestMapping("updateId")
    @ResponseBody
    public Result updateId( Long id){
        try {
            TbItemCat c =cs.updateId(id);
            return Result.OK().setData(c);
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(TbItemCat c){
        try {
            Integer i  =cs.update(c);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();//打印失败原因
            return Result.ER();
        }
    }
}
